package com.winz.cultureapp.activity;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mine.photohandler.adapter.PicFrameAdapter;
import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.BookFixBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.FileUtil;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 非固定场馆预定
 */
public class BookStadium_NoActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.et_name)
    EditText mEt_name;
    @BindView(R.id.et_company)
    EditText mEt_company;
    @BindView(R.id.et_use)
    EditText mEt_use;
    @BindView(R.id.et_phone)
    EditText mEt_phone;
    @BindView(R.id.tv_file)
    TextView mTv_file;
    @BindView(R.id.ll_file)
    LinearLayout mLl_file;

    private String id;
    private String title;

    private File file;

    @Override
    protected int contentView() {
        return R.layout.activity_book_stadium_no;
    }

    @Override
    protected void initViews() {
        setStatusBar(getResources().getColor(R.color.base_color));
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                id = intent.getExtras().getString(Constants.KEY.STADIUM.ITEM_ID);
                title = intent.getExtras().getString(Constants.KEY.STADIUM.ITEM_TITLE);
                mTv_top_title.setText(title);

            }
        }
    }

    @OnClick({R.id.fl_back,R.id.tv_submit,R.id.tv_upload,R.id.iv_delete})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_back:
                finish();
                break;
            case R.id.tv_upload:
                showFileChooser();
                break;
            case R.id.iv_delete:
                file = null;
                mTv_file.setText("");
                mLl_file.setVisibility(View.GONE);
                break;
            case R.id.tv_submit:
                if (isAllRight()){
                    submit();
                }
                break;
        }
    }

    /**
     * 提交
     */
    private void submit() {
        showProgressDialog();
        HttpUitls.onPostByToken("unit_book_unfix",
                SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                RequestFactory.getInstance().unit_book_unfix(getEditTextStr(mEt_name),getEditTextStr(mEt_phone),getEditTextStr(mEt_company),getEditTextStr(mEt_use),id,file),
                new OnResponeListener<BookFixBean>() {
            @Override
            public void onSuccess(BookFixBean bean) {
                Logger.d("unit_book_unfix==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0){
                    showMsg("提交成功");
                    finish();
                }else {
                    showMsg(bean.msg);
                }

                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("unit_book_unfix==>>", error);

            }
        });
    }

    private boolean isAllRight() {
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))){
            showMsg("请输入您的姓名");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))){
            showMsg("请输入您的工作单位,如果是个人就填个人");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))){
            showMsg("请输入您的用途");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))){
            showMsg("请输入您的手机号码");
            return false;
        }

        return true;
    }

    private static final int FILE_SELECT_CODE = 0;
    private void showFileChooser() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            showMsg("Please install a File Manager.");
        }
    }

    /**
     * 数据返回
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FILE_SELECT_CODE:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    Uri uri = data.getData();
                    file = FileUtil.getFileFromUri(uri, this);
                    if (file != null || file.exists()){
                        mLl_file.setVisibility(View.VISIBLE);
                        mTv_file.setText(file.getName());
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
